import redis

from config.env import RedisConfig


class RedisManager:
    _pool_dict: dict[tuple[str, int, int, str], redis.ConnectionPool] = {}

    def __init__(
        self,
        host: str = RedisConfig.redis_host,
        port: int = RedisConfig.redis_port,
        password: str = RedisConfig.redis_password,
        db: int = RedisConfig.redis_database,
        decode_responses: bool = False,
        max_connections: int = RedisConfig.redis_max_connections,
    ):
        pool_key = (host, port, db, password)
        if pool_key not in self._pool_dict:
            self._pool_dict[pool_key] = redis.ConnectionPool(
                host=host,
                port=port,
                password=password,
                db=db,
                decode_responses=decode_responses,
                max_connections=max_connections,
            )
        self._r = redis.Redis(connection_pool=self._pool_dict[pool_key])
        self._ping()

    def get_redis(self) -> redis.Redis:
        return self._r

    def _ping(self):
        try:
            self._r.ping()
        except BaseException as e:
            print(self._pool_dict)
            raise e


redis_client = RedisManager().get_redis()

if __name__ == "__main__":
    print(redis_client.get("test"))
